package com.koelo.kiss.re.interpreter;

import com.koelo.kiss.re.parser.IParser;
import com.koelo.kiss.re.util.Context;
import com.koelo.kiss.re.util.IContext;
import com.koelo.kiss.re.util.IMessage;
import com.koelo.kiss.re.util.IRule;
import com.koelo.kiss.re.util.Session;

public abstract class AbstractInterpreter 
	implements IInterpreter {

	private IContext context;
	private IParser parser;
	private IRule rule;

	public AbstractInterpreter(IContext context, IParser parser) {
		
		this.context = context;
		this.parser = parser;
	}

	public AbstractInterpreter(IParser parser) {

		this(new Context(), parser);
	}
	
	@Override
	public final void init() {
		
		this.rule = this.parser.init(this);
	}

	@Override
	public final boolean isHotSwapMode() {
		
		return System.getProperty("hotswapmode") != null && System.getProperty("hotswapmode").toLowerCase().equals("true") ? true : false;
	}

	@Override
	public final String[] getHotSwapPath() {
		
		return System.getProperty("hotswappath") != null ? System.getProperty("hotswappath").split(";") : new String[] {};
	}

	@Override
	public final void setContext(IContext context) {
		
		this.context = context;
	}

	@Override
	public final IContext getContext() {
		
		return this.context;
	}
	
	@Override
	public final boolean eval(IMessage message) {

		if (this.rule == null) {
			init();
		}
		return this.rule.eval(getContext(), new Session(), message);
	}
}
